// packageG/mycomponent/businessCard/businessCard.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();

Component({
  behaviors: [location],
  /**
   * 组件的属性列表
   */
  properties: {
   datas: {
     type: null,
   },
   component_id: {
     type: null,
   },
   page_id: {
    type: null,
   }
  },

  /**
   * 组件的初始数据
   */
  data: {
    cardData: {},
   area_name: '',
   bacImage: '',
   imgPath: app.globalData.imgPath,
  },
  ready() {
   const needLocation = ["4"];//需要请求定位的规则
    const isNeedLocation = this.data.datas.user.some(item => needLocation.includes(item.type));
    if(isNeedLocation) {
      this._getLocation((res, e) => {
        let point = app.qqMapTransBMap(parseFloat(e.lng),parseFloat(e.lat));
        this.getBusinessUser({lng: point.lng,lat: point.lat});
      });
    } else {
      this.getBusinessUser();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    gotoBusiness() {
      wx.navigateTo({
        url: "/packageB/member/business_card/BusinessCard/BusinessCard" + "?card_id=" + this.data.cardData.card_id + "&mark=card"
      });
    },
    getBusinessUser(extinfo={}){
      let shrID = app.globalData.mid;//分享者id
      let urlStr = app.getNetAddresss("plugin.business-card.frontend.controllers.card.card.getDecorate");
      let json = { component_id: this.data.component_id,decorate_id: this.data.page_id,  ...extinfo };
      if(shrID) json.shrID = shrID;
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            let info = {};
            info.cardData = res.data||null;
            if(info.cardData.areas) info.area_name = info.cardData.areas.join("") + info.cardData.address;
            info.bacImage = `background-image: url(${info.cardData.thumb || (this.data.imgPath + 'plugin/business_card/business_card_bg.png')})`;
            this.setData(info);
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
    changeLike(type) {
      app._getNetWork({
        url: app.getNetAddresss('plugin.business-card.frontend.controllers.action.card-action.index'),
        data: { card_id: this.data.cardData.card_id, card_type: 1 },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.setData({
              'cardMessage.card_status.collected': res.data,
              'cardMessage.collection': res.data?this.data.cardData.collection ++ : this.data.cardData.collection--
            });
          }else {
            app.tips('本人名片不能进行该操作');
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    },
  }
})